﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;

namespace Nomad.WebSync.Extensions {
    public static class ExceptionExtensions {
        public static string SoapMessage(this Exception value) {
            if (value is SoapException) {
                var index = value.Message.IndexOf("--->");
                var resultMessage = value.Message.Substring(index + 4);

                index = resultMessage.IndexOf(":");
                if (index > 0) {
                    resultMessage = resultMessage.Substring(index + 1);

                    index = resultMessage.IndexOf("\n   at ");
                    resultMessage = resultMessage.Substring(0, index);
                }

                return resultMessage.Trim();
            }

            return value.Message;
        }
    }
}
